package cn.zhouchao.org.controller;

import cn.zhouchao.basic.util.ExcelUtil;
import cn.zhouchao.org.dto.SettlementAgainDto;
import cn.zhouchao.org.dto.ShopRegisterDto;
import cn.zhouchao.org.dto.shopAuditLogDto;
import cn.zhouchao.org.service.IShopService;
import cn.zhouchao.org.domain.Shop;
import cn.zhouchao.org.query.ShopQuery;
import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.system.domain.SystemRecord;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id")Long id){
        return shopService.findById(id);
    }

    /**
     * 批量删除多个对象
     * 携带的时ids集合，作为对象放在RequestBody请求体中
     * 故与删除不同，就不在路径上+参数，而是通过请求体携带对象（多个）
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        shopService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> findAll(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryByPage(@RequestBody ShopQuery query){
        return shopService.queryByPage(query);
    }

    /**
     * 店铺入驻
     * @param dto
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopRegisterDto dto){
        shopService.settlement(dto);
        return AjaxResult.me().success();
    }

    /**
     * 店铺重新入驻
     * @param dto
     * @return
     */
    @PostMapping("/settlement/again")
    public AjaxResult settlementAgain(@RequestBody ShopRegisterDto dto){
        shopService.settlementAgain(dto);
        return AjaxResult.me().success();
    }

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody shopAuditLogDto dto) throws MessagingException {//shopId  note
        shopService.auditPass(dto);
        return AjaxResult.me().success();
    }
    /**
     * 审核驳回
     * @param dto
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody shopAuditLogDto dto){//shopId  note
        shopService.reject(dto);
        return AjaxResult.me().success();
    }


    /**
     * 店铺激活
     * @param dto
     * @return
     */
    @GetMapping("/activity/{shopId}")
    public AjaxResult auditReject(@PathVariable Long shopId){
        shopService.activity(shopId);
        return AjaxResult.me().success();
    }

    /**
     * Execl导出
     * @param resp
     */
    @GetMapping("/excel/export")
    public void exportExcel(HttpServletResponse resp){
        List<Shop> shops = shopService.findAll();
        //调用Excel工具类，参数(导出数据，标题，seetNmae名称，excel文件名，响应)
        ExcelUtil.exportExcel(shops,"","店铺信息",Shop.class,"店铺信息.xlsx",resp);
    }

    /**
     * Execl导入
     * @param resp
     */
    @PostMapping("/excel/import")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //调用Excel工具类，参数(导出数据，标题，seetNmae名称，excel文件名，响应)
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        //批量新增方法
//        shopService.batchAddShop(shops);
        shops.forEach(System.out::println);

    }


}
